﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OpenFlashChart;
using ToolTip = OpenFlashChart.ToolTip;
using ToolTipStyle = OpenFlashChart.ToolTipStyle;
using System.Collections;
using System.Text;

namespace PeisongWeb.admin
{
    public partial class companyDataAnalysisSupply : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("EF", 1))
                {
                    ddlCompanyBind();
                    RenderChart();
                }
                else
                {
                    Library.Script.ClientMsgUrl("您没有权限操作!","index.aspx");
                    return;
                }
            }
        }

        private void ddlCompanyBind()
        {
            DataTable dt = logic.company.listWithCompanyType("1");
            ddlCompany.DataSource = dt;
            ddlCompany.DataBind();
        }

        protected void btnAnalysis_Click(object sender, EventArgs e)
        {
            RenderChart();
        }

        private void RenderChart()
        {
            int companyId = 0;
            int.TryParse(ddlCompany.SelectedValue, out companyId);
            int year = DateTime.Now.Year;
            int.TryParse(ddlYear.SelectedValue, out year);

            DataTable dt = new DataTable();

            if (radModeM.Checked)
            {
                dv1.Visible = true;
                dt = logic.company.listCompanyDataAnalysisSupplyByCriteria(companyId, year);

                rpt_list.DataSource = dt;
                rpt_list.DataBind();

                RenderChart(dt);
            }
        }

        private void RenderChart(DataTable dt)
        {
            OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();

            ArrayList data1 = new ArrayList();
            IList<string> weekLabels = new List<string>();

            foreach (DataRow row in dt.Rows)
            {
                data1.Add(new LineDotValue(Convert.ToDouble(row["totalAmount"]), string.Format("{0}月供应量{1:F2}", row["month"], row["totalAmount"]), "#fe0"));
                weekLabels.Add(row["month"].ToString());
            }

            OpenFlashChart.LineHollow line1 = new LineHollow();
            line1.Values = data1;
            line1.HaloSize = 0;
            line1.Width = 2;
            line1.DotSize = 5;
            line1.Text = "月供应量";

            line1.Tooltip = "提示：#val#";

            chart.X_Axis.Labels.SetLabels(weekLabels);

            chart.AddElement(line1);

            chart.Title = new Title(string.Format("{0}{1}年月供应量", ddlCompany.SelectedItem.Text, ddlYear.SelectedItem.Text));
            chart.Y_Axis.SetRange(0,300000, 50000);
            chart.Tooltip = new ToolTip("全局提示：#val#");
            chart.Tooltip.Shadow = true;
            chart.Tooltip.Colour = "#e43456";
            chart.Tooltip.MouseStyle = ToolTipStyle.CLOSEST;
            OpenFlashChartControl1.EnableCache = false;
            OpenFlashChartControl1.Chart = chart;
        }
    }
}
